package com.qf.index.gcrdo;


import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qf.index.gcrdo.httpclient.SmallTool;
import lombok.extern.slf4j.Slf4j;

import java.util.List;


@Slf4j
public class CheckGeneParse {

    public static void main(String[] args) throws Exception {


        // 校验基因 通过网址 genenames
        // 药物，数字代号：1; 基因，数字代号：2; 通路，数字代号：3; 疾病，数字代号：4
        Db use = Db.use(DSFactory.get("localdb"));
        List<Entity> query = use.query("select * from word_and_code_set_map_new where type = '基因' and tongyi_back = '不是' and genge is null ");
        SmallTool.printMessError("查询到的数量："+query.size());
        for (Entity entity : query) {
            String check2 = entity.getStr("check2");
            if (StringUtil.isNull(check2)) {
                Integer id = entity.getInt("id");
                String key = entity.getStr("word");
                JSONObject jsonObject = JSONObject.parseObject(check2);
                if (jsonObject != null) {
                    JSONObject jsonObject1 = jsonObject.getJSONObject("suggest");
                    if (jsonObject1 != null) {
                        JSONObject jsonObject4 = jsonObject1.getJSONObject("symbolSuggester");
                        JSONObject jsonObject2 = jsonObject4.getJSONObject(key);
                        if (jsonObject2 != null) {
                            if (jsonObject2.getInteger("numFound").intValue() == 0){
                                // 没有找到
                                entity.set("genge", "不是");
                                use.insertOrUpdate(entity, "id");

                            }else if (jsonObject2.getInteger("numFound").intValue() == 1){
                                // 准确且正确唯一
                                JSONArray suggestions = jsonObject2.getJSONArray("suggestions");
                                if (suggestions!= null) {
                                    JSONObject jsonObject3 = suggestions.getJSONObject(0);
                                    if (jsonObject3 != null) {
//                                    entity.set("genge", JSONObject.toJSONString(jsonObject3));
                                        entity.set("genge", "是");
                                        use.insertOrUpdate(entity, "id");
                                    }
                                }
                            }else {
                                System.out.println(id+"不唯一");
                                JSONArray suggestions = jsonObject2.getJSONArray("suggestions");
                                boolean isFind = false;
                                if (suggestions != null) {
                                    T:
                                    for (int i = 0; i < suggestions.size(); i++) {
                                        JSONObject jsonObject3 = suggestions.getJSONObject(i);
                                        if (jsonObject3 != null) {
                                            if (jsonObject3.getString("term").toLowerCase().equals(key.toLowerCase())) {
                                                // 循环遍历找到了
                                                System.out.println("循环遍历找到了");
                                                isFind = true;
//                                            entity.set("genge", JSONObject.toJSONString(jsonObject3));
                                                entity.set("genge", "是");
                                                use.insertOrUpdate(entity, "id");
                                                break T;
                                            }
                                        }
                                    }
                                }
                                if (!isFind) {
                                    System.out.println("word is error");
                                    entity.set("genge", "word is error");
                                    use.insertOrUpdate(entity, "id");
                                }
                            }
                        }
                    }
                }
            }else {
                entity.set("genge", "不是");
                use.insertOrUpdate(entity, "id");
            }

        }

    }





}


